﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

    public class Tool
    {
        /// <summary>
        /// 默认值
        /// </summary>
        /// <param name="typename"></param>
        /// <returns></returns>
        public static string GetTypeValue(string typename, string stereotype)
        {
            typename = typename.ToLower();
            stereotype = stereotype.ToLower();
            if (stereotype=="enum"||stereotype=="bool")
            {
                return "=-1";
            }
            else if (typename.Equals("money"))
            {
                return "= decimal.MinValue";
            }
            else if (typename.Equals("int"))
            {
                return "=int.MinValue";
            }
            else if (typename.Equals("datetime"))
            {
                return "=DateTime.MinValue";
            }
            else if (typename.StartsWith("nvarchar") || typename.StartsWith("varchar"))
            {
                return "= \"\"";
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 类型转换
        /// </summary>
        /// <param name="typename"></param>
        /// <returns></returns>
        public static string GetTypeName(string typename)
        {
            typename = typename.ToLower();
            if (typename.StartsWith("nvarchar") || typename.StartsWith("varchar"))
            {
                return "String";
            }
            else if (typename.StartsWith("text"))
            {
                return "String";
            }
            else if (typename.Equals("money"))
            {
                return "Decimal";
            }
            else if (typename.Equals("datetime"))
            {
                return "DateTime";
            }

            return typename;
        }

        /// <summary>
        /// 类型转换
        /// </summary>
        /// <param name="typename"></param>
        /// <returns></returns>
        public static string GetDbTypeName(string typename)
        {
            typename = typename.ToLower();
            if (typename.StartsWith("nvarchar") || typename.StartsWith("varchar"))
            {
                return "String";
            }
            else if (typename.StartsWith("text"))
            {
                return "String";
            }
            else if (typename.Equals("money"))
            {
                return "Decimal";
            }
            else if (typename.Equals("int"))
            {
                return "Int32";
            }
            else if (typename.Equals("datetime"))
            {
                return "DateTime";
            }

            return typename;
        }

        /// <summary>
        /// 类型转换
        /// </summary>
        /// <param name="typename"></param>
        /// <returns></returns>
        public static string GetTypeName(string typename, string stereotype, string className,string code)
        {
            typename = typename.ToLower();
            if (stereotype.Equals("enum"))
            {
                return "yq.Enums." + className + "." + code;
            }
            else if (stereotype.Equals("bool"))
            {
                return "yq.Enums.BooleanType";
            }
            else if (typename.StartsWith("nvarchar") || typename.StartsWith("varchar"))
            {
                return "String";
            }
            else if (typename.StartsWith("text"))
            {
                return "String";
            }
            else if (typename.Equals("money"))
            {
                return "Decimal";
            }
            else if (typename.Equals("datetime"))
            {
                return "DateTime";
            }
             

            return typename;
        }


        /// <summary>
        /// 类型转换
        /// </summary>
        /// <param name="typename"></param>
        /// <returns></returns>
        public static string GetValidaTion(string typename, string stereotype, string className, string code, string name, bool isRequired, string length)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[Display(Name =\"" + name + "\")]\r\n");

            if (isRequired)
            {
                sb.Append("        [Required]\r\n");
            }

            typename = typename.ToLower();
            if (stereotype.Equals("bool"))
            {
                sb.Append("        [RegularExpression(RegularExpressionString.Int, ErrorMessage=\"必须输入数字\")]\r\n");
            }
            else if (typename.StartsWith("nvarchar") || typename.StartsWith("varchar"))
            {
                sb.Append("        [StringLength(" + length + ")]");
            }
            else if (typename.Equals("money"))
            {
                sb.Append("        [RegularExpression(RegularExpressionString.Double, ErrorMessage=\"必须输入数值\")]\r\n");
            }
            else if (typename.Equals("int"))
            {
                sb.Append("       [RegularExpression(RegularExpressionString.Int, ErrorMessage=\"必须输入数字\")]\r\n");
            }


            return sb.ToString();
        }


        public static string StringGet(string stereotype, string className, string code)
        {
    
            if (stereotype.Equals("enum"))
            {
                return "  (" + "yq.Enums." + className + "." + code + ")this._" + code + ";  ";
            }
            else if (stereotype.Equals("bool"))
            {
                return " (yq.Enums.BooleanType)_" + code + "; ";
            }
        
            return "  (this._"+code+"); ";
        }

        public static string StringSet(string stereotype, string className, string code)
        {
            stereotype = stereotype.ToLower();
            if (stereotype == "enum" || stereotype == "bool")
            {
                return "  _" + code + "= value.GetHashCode(); ";
            }
            else
            {
                return "  _" + code + "= value;  ";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="typename"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetConvert(string typename, string code, string stereotype, string className)
        {
            typename = typename.ToLower();
            if (stereotype.Equals("enum"))
            {
                return "  (" + "yq.Enums." + className + "." + code + ")(row[\"" + code + "\"]) ";
            }
            else if (stereotype.Equals("bool"))
            {
                return " (yq.Enums.BooleanType)(row[\"" + code + "\"]) ";
            }

            if (typename.StartsWith("nvarchar") || typename.StartsWith("varchar"))
            {
                return "row[\"" + code + "\"].ToString()";
            }
            else if (typename.Equals("text"))
            {
                return "row[\"" + code + "\"].ToString()";
            }
            else if (typename.StartsWith("datetime"))
            {
                return "Convert.ToDateTime(row[\"" + code + "\"])"; 
            }
            else if (typename.Equals("int"))
            {
                return "Convert.ToInt32(row[\"" + code + "\"])"; 
            }
            else if (typename.Equals("money"))
            {
                return "Convert.ToDecimal(row[\"" + code + "\"])"; 
            }
         
            return typename;
        }
    }
